package com.stylefeng.guns.modular.vpn.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.stylefeng.guns.modular.vpn.entity.PayRatio;
import com.stylefeng.guns.modular.vpn.entity.RealmTimeSeries;
import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Description 实时数据工具类
 * @Author Mr.God
 * @Date 2020-03-13 15:57
 **/
public class RealmTimeUtil {
    /**
     * 得到初始化图表24小时节点数据
     * @return
     */
    public static Object[] getInitSeresData(){
        Object[] data =  new Object[24];
        Arrays.fill(data,0);
        return data;
    }

    public static Integer[] getIntArray(){
        SimpleDateFormat sf = TimeUtil.getGMTDateFormat("HH");
        Integer[] data =  new Integer[Integer.parseInt(sf.format(new Date()))+1];
        Arrays.fill(data,0);
        return data;
    }


    public static Object[] getSeresData(){
        SimpleDateFormat sf = TimeUtil.getGMTDateFormat("HH");
        int tSize = Integer.parseInt(sf.format(new Date()));
        Object[] data =  new Object[tSize+1];
        Arrays.fill(data,0);
        return data;
    }

    public static Object[] getSeresData(int size){
        Object[] data =  new Object[size];
        Arrays.fill(data,0);
        return data;
    }

    public static RealmTimeSeries getOptions(String name){
        RealmTimeSeries option = new RealmTimeSeries();
        option.setName(name);
        return option;
    }

    public static List<RealmTimeSeries> getRealTimeList(int type,List<Map<String,Object>> lists){
        List<RealmTimeSeries> seriesList = new ArrayList<>();

        RealmTimeSeries todayOption = RealmTimeUtil.getOptions("今天");
        RealmTimeSeries yesterdayOption = RealmTimeUtil.getOptions("昨天");
        RealmTimeSeries sevenDaysOption = RealmTimeUtil.getOptions("7天前");
        RealmTimeSeries thirtyDaysOption = RealmTimeUtil.getOptions("30天前");

        Object[] todayData = null;
        Object[] yesterdayData = null;
        Object[] sevenDaysData = null;
        Object[] thirtyDaysData = null;

        if(lists != null && lists.size() > 0){
            if (type == 2 || type == 3){
                todayData = RealmTimeUtil.getSeresData(lists.size());
                yesterdayData = RealmTimeUtil.getSeresData(lists.size());
                sevenDaysData = RealmTimeUtil.getSeresData(lists.size());
                thirtyDaysData = RealmTimeUtil.getSeresData(lists.size());
            }else{
                todayData = RealmTimeUtil.getSeresData();
                yesterdayData = RealmTimeUtil.getSeresData();
                sevenDaysData = RealmTimeUtil.getSeresData();
                thirtyDaysData = RealmTimeUtil.getSeresData();
            }


            for (Map<String,Object> m:lists){
                todayData[Integer.parseInt((String) m.get("regTime"))] = m.get("todayRegCount");
                yesterdayData[Integer.parseInt((String) m.get("regTime"))] =  m.get("yesterdayRegCount");
                sevenDaysData[Integer.parseInt((String) m.get("regTime"))] = m.get("sevenDaysRegCount");
                thirtyDaysData[Integer.parseInt((String) m.get("regTime"))] =  m.get("thirtyDaysRegCount");
            }
        }else{
            todayData = RealmTimeUtil.getInitSeresData();
            yesterdayData = RealmTimeUtil.getInitSeresData();
            sevenDaysData = RealmTimeUtil.getInitSeresData();
            thirtyDaysData = RealmTimeUtil.getInitSeresData();
        }

        todayOption.setData(todayData);
        yesterdayOption.setData(yesterdayData);
        sevenDaysOption.setData(sevenDaysData);
        thirtyDaysOption.setData(thirtyDaysData);

        seriesList.add(todayOption);
        seriesList.add(yesterdayOption);
        seriesList.add(sevenDaysOption);
        seriesList.add(thirtyDaysOption);

        return seriesList;
    }

    /**
     * 转换查询到的图表--新用户付费信息
     * @param lists
     * @return
     */
    public static List<Map<String,Object>> payRatioChange(List<PayRatio> lists){
        if(lists == null || lists.size() == 0){
            return null;
        }

        List<Map<String,Object>> ratioList = new ArrayList<>();
        lists.forEach(r->{
            Map<String,Object> ratio = new HashMap<>();
            ratio.put("regTime",r.getPayTime());
            //今天
            if(r.getPayment1Count() == null
                    || r.getPayment1Count() == 0
                    || r.getTotal1Count() == null
                    || r.getTotal1Count() == 0){
                ratio.put("todayRegCount",0);
            }
            else{
                ratio.put("todayRegCount",NumberUtil.formatDouble((double) r.getPayment1Count() / r.getTotal1Count()));
            }
            //前天
            if(r.getPayment2Count() == null
                    || r.getPayment2Count() == 0
                    || r.getTotal2Count() == null
                    || r.getTotal2Count() == 0){
                ratio.put("yesterdayRegCount",0);
            }
            else{
                ratio.put("yesterdayRegCount",NumberUtil.formatDouble((double) r.getPayment2Count() / r.getTotal2Count()));
            }
            //7天
            if(r.getPayment3Count() == null
                    || r.getPayment3Count() == 0
                    || r.getTotal3Count() == null
                    || r.getTotal3Count() == 0){
                ratio.put("sevenDaysRegCount",0);
            }
            else{
                ratio.put("sevenDaysRegCount",NumberUtil.formatDouble((double) r.getPayment3Count() / r.getTotal3Count()));
            }
            //30天
            if(r.getPayment4Count() == null
                    || r.getPayment4Count() == 0
                    || r.getTotal4Count() == null
                    || r.getTotal4Count() == 0){
                ratio.put("thirtyDaysRegCount",0);
            }
            else{
                ratio.put("thirtyDaysRegCount",NumberUtil.formatDouble((double) r.getPayment4Count() / r.getTotal4Count()));
            }
            ratioList.add(ratio);
        });

        return ratioList;
    }
}
